#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-etc
# REQUIRE: earlykld ix-syncdisks
# BEFORE: fsck

. /etc/rc.subr

copy_templates()
{
	# Copy over FreeNAS specific /etc template files
	for file in $(find /etc | grep '.template$' 2>/dev/null)
	do
		nfile=$(echo $file | sed 's|.template||g')
		cp ${file} ${nfile}
		if [ $? -ne 0 ] ; then
			echo "WARNING: Failed to copy template file $file -> $nfile"
		fi

		# Special handling for login.conf file that needs cap_mkdb run
		if [ "$nfile" = "/etc/login.conf" ] ; then
			cap_mkdb /etc/login.conf
		fi
	done
}

generate_etc()
{
	copy_templates

	LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/midclt call --job true --job-print description certificate.dhparam_setup > /dev/null
	LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/midclt call etc.generate_checkpoint initial > /dev/null

	# etc rc plugin will generate rc.conf.freenas which needs to be re-read by /etc/rc script
	killall -ALRM sh
}

name="ix-etc"
start_cmd='generate_etc'
stop_cmd=':'

load_rc_config $name
run_rc_command "$1"
